﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace UdrinkWater_Project.Admin
{
    public partial class MaterialStockOut : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        DateTime OrderM_ID = DateTime.Now;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string T1 = OrderM_ID.ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"));
                string T2 = OrderM_ID.ToString("HH:mm:ss", new System.Globalization.CultureInfo("en-US"));
                txtOrderDate.Text = T1 + " " + T2;
            }
            else
            {
                string T1 = OrderM_ID.ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"));
                string T2 = OrderM_ID.ToString("HH:mm:ss", new System.Globalization.CultureInfo("en-US"));
                txtOrderDate.Text = T1 + " " + T2;
            }
        }

        protected void btSaveAndReport_Click(object sender, EventArgs e)
        {
            string sqltext = @"Insert into TB_Material_StockOUT
                              values (@da,@Mid,@Mcount,@act)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@da", Convert.ToDateTime(txtOrderDate.Text));
            sqlcomm.Parameters.AddWithValue("@Mid", ddlist.SelectedValue);
            sqlcomm.Parameters.AddWithValue("@Mcount", Convert.ToInt32(txtTotal.Text));
            sqlcomm.Parameters.AddWithValue("@act", txtAct.Text);
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

            Response.Redirect("~/Officer/MaterialStockOut.aspx");
        }

        protected void ASPxGridView2_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
        {
            DateTime Mdate = Convert.ToDateTime(e.CommandArgs.CommandArgument.ToString());
            SqlConnection con = new SqlConnection(Connection);
            con.Open();
            string sql = @"delete TB_Material_StockOUT where M_Date = '" + Mdate.ToString(new System.Globalization.CultureInfo("en-US")) + "'";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Redirect("~/Admin/MaterialStockOut.aspx");
        }
    }
}